package com.sf.algo.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpUtil {
	
	private HttpUtil(){
		
	}

	/**
	 * get请求
	 * @param url
	 * @param params
	 * @param clazz
	 * @return
	 */
	public static <T> T doGet(String url,Map<String,String> params,Class<T> clazz){
		HttpClientBuilder builder = HttpClientBuilder.create();
		CloseableHttpClient client = builder.build();
		HttpGet get = new HttpGet(url);
		if(params != null){
			List<NameValuePair> pairs = buildParams(params);
			try {
				UrlEncodedFormEntity form = new UrlEncodedFormEntity(pairs, "UTF-8");
				String tail = EntityUtils.toString(form);
				System.out.println(url+"?"+tail);
				get.setURI(new URI(url+"?"+tail));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
		}
		try {
			HttpResponse response = client.execute(get);
			HttpEntity entity = response.getEntity();
			String content = EntityUtils.toString(entity, "UTF-8");
			return JacksonUtil.toObject(content, clazz);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(client != null)
					client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * post请求
	 * @param url
	 * @param params
	 * @param clazz
	 * @return
	 */
	public static <T> T doPost(String url,Map<String,String> params,Class<T> clazz){
		HttpClientBuilder builder = HttpClientBuilder.create();
		CloseableHttpClient client = builder.build();
		HttpPost post = new HttpPost(url);
		if(params != null){
			List<NameValuePair> pairs = buildParams(params);
			try {
				UrlEncodedFormEntity form = new UrlEncodedFormEntity(pairs, "UTF-8");
				post.setEntity(form);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		HttpResponse response;
		try {
			response = client.execute(post);		
			HttpEntity entity = response.getEntity();
			String content = EntityUtils.toString(entity, "UTF-8");
			return JacksonUtil.toObject(content, clazz);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(client != null)
					client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static <T> T doApplicationJson(String url,Map<String,String> params,Class<T> clazz){
		HttpClientBuilder builder = HttpClientBuilder.create();
		CloseableHttpClient client = builder.build();
		HttpPost post = new HttpPost(url);
		post.addHeader("Content-type","application/json; charset=utf-8");  
		post.setHeader("Accept", "application/json");  
		if(params != null){
			try {
				post.setEntity(new StringEntity(JacksonUtil.toJSON(params)));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		HttpResponse response;
		try {
			response = client.execute(post);		
			HttpEntity entity = response.getEntity();
			String content = EntityUtils.toString(entity, "UTF-8");
			return JacksonUtil.toObject(content, clazz);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(client != null)
					client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * http参数
	 * @param params
	 * @return
	 */
	private static List<NameValuePair> buildParams(Map<String,String> params){
		if(params == null || params.keySet().size() == 0)
			return null;
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		for(String key : params.keySet()){
			BasicNameValuePair pair = new BasicNameValuePair(key,params.get(key));
			pairs.add(pair);
		}
		return pairs;
	}
}
